package com.burgeon.backend.SHOP.service.Impl;

import com.burgeon.backend.SHOP.entity.Favoritesgoods;
import com.burgeon.backend.SHOP.entity.Goods;
import com.burgeon.backend.SHOP.mappers.FavoritesMapper;
import com.burgeon.backend.SHOP.service.FavoritesService;
import com.burgeon.backend.SHOP.service.GoodsService;
import com.burgeon.backend.SHOP.service.ex.ErrorDataExceptionApp;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.logging.Log;
import java.util.ArrayList;

@Service
public class FavoritesServiceimpl implements FavoritesService {

    private final static Log logger = LogFactory.getLog(FavoritesService.class);

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private FavoritesMapper favorites;

    /**
     * 判断用户商品是否被收藏  弃用
     * @param uid
     * @param gid
     * @return
     */
    @Override
    public String getGoodsFavorites(String uid, String gid) {
//        Goods goods = goodsService.findGoodsById(gid);
//        Favoritesgoods favoritesgoods = favorites.findFavorite(uid,gid);
//        if (null == favoritesgoods){
//            goods.setIsFavorites("N");
//        }else {
//            goods.setIsFavorites("Y");
//        }
        return null;
    }

    @Override
    public void addfavoritesservice(String gid,String uid,String token) throws ErrorDataExceptionApp {
        try {
            Goods goods = goodsService.findGoodsById(gid,uid,"1","N");
            Favoritesgoods favoritesgoodss = new Favoritesgoods();
            favoritesgoodss.setGoods_id(goods.getId());
            //favoritesgoodss.setDiscounts(goods.getDiscounts());
            favoritesgoodss.setItem_img(goods.getImg());
            favoritesgoodss.setIntegral(goods.getIntegral());
            favoritesgoodss.setName(goods.getName());
            favoritesgoodss.setPrice(goods.getPrice());
            favoritesgoodss.setType(goods.getType());
            favoritesgoodss.setUid(uid);
            favoritesgoodss.setToken(token);
            favorites.addfavoritesgoods(favoritesgoodss);

        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new ErrorDataExceptionApp(e.getMessage());
        }
    }
    @Override
    public void delect(String id)throws ErrorDataExceptionApp {
        try {
            favorites.delect(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new ErrorDataExceptionApp(e.getMessage());
        }
    }
    @Override
    public ArrayList<Favoritesgoods> select(String token, String uid)throws ErrorDataExceptionApp {
        try {
            ArrayList<Favoritesgoods> select = favorites.select(token, uid);
            return select;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new ErrorDataExceptionApp(e.getMessage());
        }
    }
}